<template>
  <div style="padding:30px;">
    <Breadcrumb />
    <el-card class="box-card" style="margin-top:15px;">
      <div class="text item">
        <div style="margin-top: 15px">
          <el-input
            v-model="query.query"
            style="width:33%;"
            :clearable="true"
            placeholder="请输入内容"
            class="input-with-select"
            @clear="empty"
          >
            <el-button slot="append" icon="el-icon-search" @click="querys" />
          </el-input>
          <el-button
            type="primary"
            style="margin-left:20px;"
            class="addUser"
            @click="addGoods"
          >添加商品</el-button>
          <el-table
            :data="goodsList"
            style="width: 100%;margin:15px 0;"
            border
          >
            <el-table-column
              type="index"
              label="#"
              width="50"
            />
            <el-table-column
              prop="goods_name"
              label="商品名称"
            />
            <el-table-column
              prop="goods_price"
              label="商品价格(元)"
            />
            <el-table-column
              prop="goods_weight"
              label="商品重量"
            />
            <el-table-column
              prop="add_time"
              label="创建时间"
            />
            <el-table-column
              label="操作"
            >
              <template slot-scope="scope">
                <el-row class="btns">
                  <el-button icon="el-icon-edit" type="primary" @click="editUserInfo(scope.row)" />
                  <el-button icon="el-icon-delete" type="danger" @click="deleteUser(scope.row)" />
                </el-row>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <el-pagination
        :current-page="query.pagenum"
        :page-sizes="[5, 10, 20]"
        :page-size="query.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>

<script>
import { goodsList, deleteGoods } from '@/api/goods.js'

export default {
  data() {
    return {
      goodsList: [],
      query: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      total: 0
    }
  },

  created() {
    this.loadGoodsList()
  },

  methods: {
    // 获取商品列表
    async loadGoodsList() {
      const res = await goodsList(this.query)
      this.goodsList = res.goods
      this.total = res.total
    },
    addGoods() {
      this.$router.push('/goods/add')
    },
    handleSizeChange(val) {
      this.query.pagesize = val
      this.loadGoodsList()
    },
    handleCurrentChange(val) {
      this.query.pagenum = val
      this.loadGoodsList()
    },
    // 删除用户
    deleteUser(data) {
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteGoods(data.goods_id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.loadGoodsList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 编辑用户
    editUserInfo() {},
    // 筛选
    querys() {
      this.loadGoodsList()
    },
    empty() {
      console.log(1)
      this.query.query = ''
      this.loadGoodsList()
    }
  }
}
</script>

<style scoped lang='scss'>
.btns {
    display: flex;
    .el-button {
      width: 44px;
      height: 28px;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
</style>

